package com.sne.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.entity.Result;
import com.sne.sys.entity.SysFile;
import com.sne.sys.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.*;
import java.net.URLEncoder;

/**
 * <p>
 * 文件管理 前端控制器
 * </p>
 *
 * @author fanghh
 * @since 2020-01-10
 */
@Slf4j
@RestController
@RequestMapping("/sys/sys-file")
@Api(tags = "文件上传管理")
public class SysFileController {

    @Autowired
    private ISysFileService iSysFileService;

    @ApiOperation(value = "文件管理分页")
    @GetMapping("/page")
    public Result page(Page page, SysFile sysFile) {
        return Result.success(iSysFileService.page(page, Wrappers.query(sysFile)));
    }

    @ApiOperation(value = "保存文件上传")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SysFile sysFile) {
        return Result.success(iSysFileService.save(sysFile));
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(iSysFileService.removeById(id));
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    @SneakyThrows
    public Result upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("fileSpace") String fileSpace) {
        if (multipartFile.isEmpty()) {
            return Result.failed();
        }
        if (StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return Result.failed("文件参数有问题");
        }
        File fileUpload = new File(fileSpace, multipartFile.getOriginalFilename());
        if (fileUpload.exists()) {
            return Result.failed("上传的文件已存在");
        }
        multipartFile.transferTo(fileUpload);
        return Result.success();
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/download/{id}")
    @SneakyThrows
    public ResponseEntity download(@PathVariable("id") Long id) {
        SysFile sysFile = iSysFileService.getById(id);
        File file = new File(sysFile.getFileName());
        return ResponseEntity
                .ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(file.length())
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"))
                .body(file);
    }

}

